﻿using System;
using System.Runtime.Serialization;
using Catel.Articles.Base.Data.Attributes;
using Catel.Data;

namespace Catel.Articles._01___Data_handling.Models
{
    using System.Collections.Generic;

    /// <summary>
    /// ValidatingObject Data object class which fully supports serialization, property changed notifications,
    /// backwards compatibility and error checking.
    /// </summary>
    [Example("Validating object", "Shows how to use validation", 
        SourceFileName = "/Models/ValidatingObject.cs")]
    [Serializable]
    public class ValidatingObject : DataObjectBase<ValidatingObject>
    {
        #region Variables
        #endregion

        #region Constructor & destructor
        /// <summary>
        /// Initializes a new object from scratch.
        /// </summary>
        public ValidatingObject() { }

        /// <summary>
        /// Initializes a new object based on <see cref="SerializationInfo"/>.
        /// </summary>
        /// <param name="info"><see cref="SerializationInfo"/> that contains the information.</param>
        /// <param name="context"><see cref="StreamingContext"/>.</param>
        protected ValidatingObject(SerializationInfo info, StreamingContext context)
            : base(info, context) { }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the field warning property.
        /// </summary>
        public string FieldWarning
        {
            get { return GetValue<string>(FieldWarningProperty); }
            set { SetValue(FieldWarningProperty, value); }
        }

        /// <summary>
        /// Register the FieldWarning property so it is known in the class.
        /// </summary>
        public static readonly PropertyData FieldWarningProperty = RegisterProperty("FieldWarning", typeof(string), "Invalid field value");

        /// <summary>
        /// Gets or sets the field error property.
        /// </summary>
        public string FieldError
        {
            get { return GetValue<string>(FieldErrorProperty); }
            set { SetValue(FieldErrorProperty, value); }
        }

        /// <summary>
        /// Register the FieldError property so it is known in the class.
        /// </summary>
        public static readonly PropertyData FieldErrorProperty = RegisterProperty("FieldError", typeof(string), "Invalid field value");
        #endregion

        #region Methods
        protected override void ValidateFields(List<FieldValidationResult> validationResults)
        {
            if (FieldWarning == "Invalid field value")
            {
                validationResults.Add(FieldValidationResult.CreateWarning(FieldWarningProperty, "Property 'FieldWarning' is probably wrong"));
            }

            if (FieldError == "Invalid field value")
            {
                validationResults.Add(FieldValidationResult.CreateError(FieldErrorProperty, "Property 'FieldError' is wrong"));
            }
        }
        #endregion
    }
}
